package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.aopLog.OperateLog;
import com.huilian.iotbox.admin.wap.service.MerchantStoreService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.MerchantStoreDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutionException;

/**
 * @author 谢洛涛
 * @date 2021/1/11 17:34
 */
@RestController
@RequestMapping("/api/merchantStore")
public class MerchantStoreController extends UserInfoController {
    @Resource
    private MerchantStoreService merchantStoreService;

    /**
     * 修改套餐模式
     *
     * @param merchantStoreDto
     * @param request
     * @return
     */
    @PutMapping("/updatePackageModel")
    public ServerResponse updatePackageModel(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.updatePackageModel(merchantStoreDto);
    }


    /**
     * 修改计量单位
     *
     * @param merchantStoreDto
     * @param request
     * @return
     */
    @PutMapping("/updateMeasuringUnit")
    public ServerResponse updateMeasuringUnit(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.updateMeasuringUnit(merchantStoreDto);
    }


    @GetMapping("/findAllMerchantStoreByUserId")
    public ServerResponse findAllMerchantStoreByMchId(MerchantStoreDto merchantStoreDto, HttpServletRequest request) throws InterruptedException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantStoreService.findAllMerchantStoreByMchId(merchantStoreDto));
    }

    @PutMapping("/updateMerchantStore")
    public ServerResponse updateMerchantStore(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserId(userInfo.getId());
        return merchantStoreService.updateMerchantStore(merchantStoreDto);
    }

    /**
     * 场地列表查询
     *
     * @param merchantStoreDto
     * @param request
     * @return
     */
    @PostMapping("/findMerchantStoreList")
    public ServerResponse findMerchantStoreList(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantStoreService.findMerchantStoreList(merchantStoreDto));
    }

    /**
     * 修改默认值.
     *
     * @param merchantStoreDto
     * @param request
     * @return
     */
    @PutMapping("/updateDefaultStatus")
    public ServerResponse updateDefaultStatus(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserId(userInfo.getId());
        return merchantStoreService.updateDefaultStatus(merchantStoreDto);
    }

    /**
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/findMerchantStoreByUserIdByStoreId")
    public ServerResponse findMerchantStoreByUserIdByStoreId(Integer id, HttpServletRequest request) {
        MerchantStoreDto merchantStoreDto = new MerchantStoreDto();
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            merchantStoreDto.setUserId(userInfo.getParentId());
        } else {
            merchantStoreDto.setUserId(userInfo.getId());
        }
        merchantStoreDto.setId(id);
        return ServerResponse.createBySuccess(merchantStoreService.findMerchantStoreByUserIdByStoreId(merchantStoreDto));
    }

    /**
     * 编辑和添加场地.
     *
     * @param merchantStoreDto
     * @param request
     * @return
     */
    @OperateLog
    @PostMapping("/save")
    public ServerResponse save(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) throws ExecutionException, InterruptedException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            merchantStoreDto.setUserId(userInfo.getParentId());
            merchantStoreDto.setSubUserId(userInfo.getId());
        } else {
            merchantStoreDto.setUserId(userInfo.getId());
        }
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.save(merchantStoreDto);
    }

    /**
     * 编辑和添加场地.
     *
     * @param merchantStoreDto
     * @param request
     * @return
     */
    @OperateLog
    @PostMapping("/applet/save")
    public ServerResponse saveByApplet(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) throws ExecutionException, InterruptedException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            merchantStoreDto.setUserId(userInfo.getParentId());
            merchantStoreDto.setSubUserId(userInfo.getId());
        } else {
            merchantStoreDto.setUserId(userInfo.getId());
        }
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.saveByApplet(merchantStoreDto);
    }

    /**
     * 查询场地名称是否有重复.
     *
     * @param storeName
     * @param request
     * @return
     */
    @GetMapping("/findByUserIdAndStoreName")
    public ServerResponse findByUserIdAndStoreName(String storeName, HttpServletRequest request) {
        MerchantStoreDto merchantStoreDto = new MerchantStoreDto();
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserId(userInfo.getId());
        merchantStoreDto.setStoreName(storeName);
        return ServerResponse.createBySuccess(merchantStoreService.findByUserIdAndStoreName(merchantStoreDto));
    }

    /**
     * 场地排行.
     *
     * @param merchantStoreDto
     * @param request
     * @return
     */
    @PostMapping("/findMerchantStoreRanking")
    public ServerResponse findMerchantStoreRanking(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
//        UserInfo userInfo = super.getLoginUserInfo(request);
//        merchantStoreDto.setUserInfo(userInfo);
//        return ServerResponse.createBySuccess(merchantStoreService.findMerchantStoreRanking(merchantStoreDto));
        //未修改sql使用时间函数
        return ServerResponse.createBySuccess();
    }

    @GetMapping("/findDefaultStore")
    public ServerResponse findDefaultStore(HttpServletRequest request) {
        MerchantStoreDto merchantStoreDto = new MerchantStoreDto();
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantStoreService.findDefaultStore(merchantStoreDto));
    }


    /**
     * 不分页场地列表查询
     *
     * @param merchantStoreDto
     * @param request
     * @return
     */
    @PostMapping("/findNoPageList")
    public ServerResponse findNoPageList(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantStoreService.findNoPageList(merchantStoreDto));
    }

    @PostMapping("/updateMessage")
    public ServerResponse updateMessage(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        return merchantStoreService.updateMessage(merchantStoreDto);
    }

    /**
     * 查询场地分账比例
     */
    @PostMapping("/findAreaLedgerProportionAndPage")
    public ServerResponse findAreaLedgerProportionAndPage(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserId(userInfo.getId());
        merchantStoreDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantStoreService.findAreaLedgerProportionAndPage(merchantStoreDto));
    }

    /**
     * 场地管理删除设备为0的场地
     */
    @PostMapping("/updateAsDelById")
    public ServerResponse updateAsDelById(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserId(userInfo.getId());
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.updateAsDelById(merchantStoreDto);
    }

    @PostMapping("/findStoreShelves")
    public ServerResponse findStoreShelves(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.findStoreShelves(merchantStoreDto);
    }

    /**
     * 获取门店列表
     */
    @GetMapping("/findMerchantStoreIdByUserId")
    public ServerResponse findMerchantStoreIdByUserId(HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        return ServerResponse.createBySuccess(merchantStoreService.findMerchantStoreIdByUserId(userInfo));
    }

    /**
     * 根据设备类型获取门店列表
     */
    @GetMapping("/findMerchantStoreIdByUserIdAndDeviceTypeId")
    public ServerResponse findMerchantStoreIdByUserIdAndDeviceTypeId(Integer deviceTypeId,HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        MerchantStoreDto merchantStoreDto = new MerchantStoreDto();
        merchantStoreDto.setDeviceTypeId(deviceTypeId);
        merchantStoreDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantStoreService.findMerchantStoreIdByUserIdAndDeviceTypeId(merchantStoreDto));
    }

    /**
     * 线下收款二维码
     */
    @GetMapping("/receiptQrCode")
    public ServerResponse receiptQrCode(MerchantStoreDto merchantStoreDto,HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.receiptQrCode(merchantStoreDto);
    }
    @GetMapping("/findAllInventoryByOutTradeNo")
    public ServerResponse findAllInventoryByOutTradeNo(MerchantStoreDto merchantStoreDto,HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(merchantStoreService.findAllInventoryByOutTradeNo(merchantStoreDto));
    }

    /**
     * 查询场地门闸设备
     * */
    @PostMapping("/findStoreGateGateDeviceNo")
    public ServerResponse findStoreGateGateDeviceNo(@RequestBody MerchantStoreDto merchantStoreDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        merchantStoreDto.setUserInfo(userInfo);
        return merchantStoreService.findStoreGateGateDeviceNo(merchantStoreDto);
    }
}
